package com.sg.business.commons.sidebar.project;

import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.ui.IViewPart;

import com.mobnut.commons.util.Utils;
import com.sg.business.model.Organization;
import com.sg.business.model.Project;
import com.sg.business.model.User;
import com.sg.business.resource.nls.Messages;
import com.sg.widgets.Widgets;
import com.sg.widgets.part.sidebar.AbstractTreeViewSideItem;
import com.sg.widgets.part.view.NavigatorPart;
import com.sg.widgets.viewer.ConditionDefinition;

public class ProjectChargerFilterBarManagement extends AbstractTreeViewSideItem
		implements ISelectionChangedListener {


	@Override
	protected String getTreeId() {
		return "organization.user.selector.pjadmin";
	}


	protected void select(Object selected) {
		IViewPart view = Widgets.getViewPart(getRelatedViewPart());
		select(view, selected);
	}

	protected String getRelatedViewPart() {
		return "project.navigator.management";
	}

	protected void select(IViewPart view, Object selected) {
		ConditionDefinition cond = getConditionDefinition(selected);
		if (view instanceof NavigatorPart) {
			if (cond == null) {
				((NavigatorPart) view).doRemoveAllFilterConditions();
			} else {
				((NavigatorPart) view)
						.doSetFilterCondition(new ConditionDefinition[] { cond });
			}
		}
	}

	private ConditionDefinition getConditionDefinition(Object selected) {
		if (selected instanceof Organization) {
			return new ConditionDefinition(Project.F_LAUNCH_ORGANIZATION,
					Messages.get().UNDERTAKE_DEPARTMENT, Utils.TYPE_STRING,
					Utils.OPERATOR_EQUAL, ((Organization) selected).get_id(),
					null);
		} else if (selected instanceof User) {
			return new ConditionDefinition(Project.F_CHARGER,
					Messages.get().CHARGER, Utils.TYPE_STRING,
					Utils.OPERATOR_EQUAL, ((User) selected).getUserid(), null);
		}
		return null;
	}

}
